<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const p1 = new Promise((resolve, reject) => {
      try {
        console.log("请求数据A")
        throw new Error("A错误")
        setTimeout(() => {
          console.log("请求A结束....");
          resolve("A数据")
        }, 1500)
      } catch (e) {
        reject(e.message)
      }
    })

    const p2 = new Promise((resolve, reject) => {
      try {
        console.log("请求数据B")
        // throw new Error("B错误")
        setTimeout(() => {
          console.log("请求B结束....");
          resolve("B数据")
        }, 2500)
      } catch (e) {
        reject(e.message)
      }
    })


    const p3 = new Promise((resolve, reject) => {
      try {
        console.log("请求数据C")
        throw new Error("C错误")
        setTimeout(() => {
          console.log("请求C结束....");
          resolve("C数据")
        }, 2000)
      } catch (e) {
        reject(e.message)
      }
    })

    /* 
      Promise.allSettled方法:
        - 接收一个数组(原则上为iterable类型)作为参数

        - allSettled方法 接收所有 promise 结果,把它们值和状态组成对象,把对象按照书写的顺序依次放入数组中作为allSettled 返回promise实例的值
        
        - allSettled 永远返回成功的状态
    */

    const result = Promise.allSettled([p1,p2,p3]);
    console.log("result", result);
    result.then(value => {
      console.log("全部完成");
    }).catch(reason => {
      console.log("有错误的", reason);
    })


    //第一遍 promise.allSettled
     const k = new Promise((resolve,reject)=>{
      try{
        console.log('请求A数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('A数据请求成功');
          resolve('A数据')
        });

      }catch(e){
        reject(e.message)
      }
     },1500);

     const k1 = new Promise((resolve,reject)=>{
      try{
        console.log('请求B数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('B数据请求成功');
          resolve('B数据')
        },2000);

      }catch(e){
        reject(e.message)
      }
     });


     const k2 = new Promise((resolve,reject)=>{
      try{
        console.log('请求C数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('C数据请求成功');
          resolve('C数据')
        },3000);

      }catch(e){
        reject(e.message)
      }
     });

    //  Promise.allSettled方法:
    //     - 接收一个数组(原则上为iterable类型)作为参数
    //     - allSettled方法 接收所有 promise 结果,把它们值和状态组成对象,把对象按照书写的顺序依次放入数组中作为allSettled 返回promise实例的值
    //     - allSettled 永远返回成功的状态
     const result1 = Promise.allSettled([k,k1,k2])
     console.log('result1',result1);
     result1.then((value)=>{
      console.log('数据全部请求成功');
     },(reason)=>{
      console.log('数据请求失败',reason);
     })



     //第二遍  promise.allSettled
     const two = new Promise((resolve,reject)=>{
      try{
        console.log('请求姓名数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('姓名数据请求成功');
          resolve('姓名数据')
        },1000);

      }catch(e){
        reject(e.message)
      }
     });
     const two1 = new Promise((resolve,reject)=>{
      try{
        console.log('请求年龄数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('年龄数据请求成功');
          resolve('年龄数据')
        },1500);

      }catch(e){
        reject(e.message)
      }
     });

     const two2 = new Promise((resolve,reject)=>{
      try{
        console.log('请求性别数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('性别数据请求成功');
          resolve('性别数据')
        },3000);

      }catch(e){
        reject(e.message)
      }
     })


      //  Promise.allSettled方法:
    //     - 接收一个数组(原则上为iterable类型)作为参数
    //     - allSettled方法 接收所有 promise 结果,把它们值和状态组成对象,把对象按照书写的顺序依次放入数组中作为allSettled 返回promise实例的值
    //     - allSettled 永远返回成功的状态
    const result2 = Promise.allSettled([two,two1,two2])
     console.log('result2',result2);
     result2.then((value)=>{
      console.log('数据全部请求成功');
     },(reason)=>{
      console.log('数据请求失败',reason);
     })


     //第三遍 promise.allSettled
     const three = new Promise((resolve,reject)=>{
      try{
        console.log('获取医院名单数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('获取医院名单数据成功');
          resolve('医院名单数据')
        },1400);

      }catch(e){
        reject(e.message)
      }
     });

     const three1 = new Promise((resolve,reject)=>{
      try{
        console.log('获取医院开销数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('获取医院开销数据成功');
          resolve('医院开销数据')
        },1300);

      }catch(e){
        reject(e.message)
      }
     });

     const three2 = new Promise((resolve,reject)=>{
      try{
        console.log('获取医院收入数据');
        // throw new Error('网络问题')
        setTimeout(()=>{
          console.log('获取医院收入数据成功');
          resolve('医院收入数据')
        },2200);

      }catch(e){
        reject(e.message)
      }
     })

    //  Promise.allSettled方法:
    //     - 接收一个数组(原则上为iterable类型)作为参数
    //     - allSettled方法 接收所有 promise 结果,把它们值和状态组成对象,把对象按照书写的顺序依次放入数组中作为allSettled 返回promise实例的值
    //     - allSettled 永远返回成功的状态
     
     const result3 = Promise.allSettled([three,three1,three2]);
     console.log('result3',result3);
     result3.then((value)=>{
      console.log('数据全部请求成功');
     },(reason)=>{
      console.log('数据请求失败',reason);
     })
    
  </script>
</body>

</html>